package midoo.server.core;

import io.netty.channel.ChannelHandlerContext;
import midoo.common.MServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by ajianzheng on 2015/4/10.
 */
public abstract class MWorker implements MServer, MOverload, MMessageConsumer, MServerUserEvent {
    protected static final Logger LOGGER = LoggerFactory.getLogger(MWorker.class);

    @Override
    public void handleOverload(KOverloadType type, Object object) {
        switch (type) {
            case MESSAGE:
            {
                MMessage msg = (MMessage) object;
                LOGGER.warn("Message overload, discard msg from:{},waiting:{},{}",
                        msg.getRemoteString(), msg.getWaitingMillis(), msg.getMessage());
                break;
            }
            case CONNECTION:
            {
                ChannelHandlerContext ctx = (ChannelHandlerContext) object;
                LOGGER.warn("Connection overload, discard connection:{}", ctx.channel().remoteAddress());
                /// TODO
                break;
            }
            default:
                LOGGER.warn("Unknown type overload:{}", type);
                break;
        }
    }

    @Override
    public String handleUserEvent(String s) {
        LOGGER.info("onUserEvent:{}", s);

        return "OK";
    }
}
